package com.audi.adapter;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

import com.audi.dto.BoxItem;
import com.audi.dto.DeviceItem;
import com.audi.dto.HistoryPlaceItem;
import com.audi.dto.PlaceItem;
import com.audi.dto.SceneItem;
import com.audi.dto.WifiItem;
import com.audi.utils.DeviceHelper;

public class SQLiteAsusNewHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "audi.db";
	private static final int DATABASE_VERSION = 2;

	// TABLE DEVICE
	private static final String TABLE_DEVICE = "device";
	private static final String KEY_DEVICE_ID = "_device_id";
	private static final String KEY_DEVICE_NAME = "_device_name";
	private static final String KEY_DEVICE_ICON = "_device_icon";
	private static final String KEY_DEVICE_STATUS = "_device_status";
	private static final String KEY_DEVICE_TYPE = "_device_type";
	private static final String KEY_DEVICE_CONTROL_VALUE = "_device_control_value";
	private static final String KEY_DEVICE_CODE = "_device_code";
	private static final String KEY_DEVICE_BOX_ID = "_device_box_id";
	private static final String KEY_DEVICE_PLACE_ID = "_device_place_id";
	private static final String KEY_DEVICE_USER_ID = "_device_user_id";
	private static final String KEY_DEVICE_IS_DELETED = "_device_is_deleted";
	private static final String KEY_DEVICE_FAVORITE = "_device_favorite";
	private static final String CREATE_TABLE_DEVICE = "CREATE TABLE "
			+ TABLE_DEVICE + "(" + KEY_DEVICE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_DEVICE_NAME
			+ " TEXT," + KEY_DEVICE_ICON + " TEXT," + KEY_DEVICE_STATUS
			+ " INTEGER," + KEY_DEVICE_TYPE + " INTEGER,"
			+ KEY_DEVICE_CONTROL_VALUE + " INTEGER," + KEY_DEVICE_CODE
			+ " TEXT," + KEY_DEVICE_BOX_ID + " INTEGER," + KEY_DEVICE_PLACE_ID
			+ " INTEGER," + KEY_DEVICE_USER_ID + " INTEGER,"
			+ KEY_DEVICE_FAVORITE + " TEXT, " + KEY_DEVICE_IS_DELETED
			+ " INTEGER DEFAULT 0"

			+ ")";

	// TABLE PLACE
	private static final String TABLE_PLACE = "place";
	private static final String KEY_PLACE_ID = "_place_id";
	private static final String KEY_PLACE_NAME = "_place_name";
	private static final String KEY_PLACE_ICON = "_place_icon";
	private static final String KEY_PLACE_CAPABILITY_TYPE = "_place_capability_type";
	private static final String KEY_PLACE_IS_DELETED = "_place_is_deleted";
	private static final String CREATE_TABLE_PLACE = "CREATE TABLE "
			+ TABLE_PLACE + "(" + KEY_PLACE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PLACE_NAME + " TEXT,"
			+ KEY_PLACE_ICON + " TEXT," + KEY_PLACE_CAPABILITY_TYPE
			+ " INTEGER," + KEY_PLACE_IS_DELETED + " INTEGER DEFAULT 0" + ")";

	// TABLE SCENE
	private static final String TABLE_SCENE = "scene";
	private static final String KEY_SCENE_ID = "_scene_id";
	private static final String KEY_SCENE_NAME = "_scene_name";
	private static final String KEY_SCENE_ICON = "_scene_icon";
	private static final String KEY_SCENE_COLOR = "_scene_color";
	private static final String KEY_SCENE_PLACE_ID = "_scene_place_id";
	private static final String KEY_SCENE_IS_DELETED = "_scene_is_deleted";
	private static final String CREATE_TABLE_SCENE = "CREATE TABLE "
			+ TABLE_SCENE + "(" + KEY_SCENE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_SCENE_NAME + " TEXT,"
			+ KEY_SCENE_ICON + " TEXT," + KEY_SCENE_COLOR + " INTEGER,"
			+ KEY_SCENE_PLACE_ID + " INTEGER," + KEY_SCENE_IS_DELETED
			+ " INTEGER DEFAULT 0" + ")";

	// TABLE SCENE_DEVICE
	private static final String TABLE_SCENE_DEVICE = "scene_device";
	private static final String KEY_SCENE_DEVICE_SCENE_ID = "_scene_device_scene_id";
	private static final String KEY_SCENE_DEVICE_DEVICE_ID = "_scene_device_device_id";
	private static final String KEY_SCENE_DEVICE_DEVICE_STATUS = "_scene_device_device_status";
	private static final String KEY_SCENE_DEVICE_IS_DELETED = "_scene_device_is_deleted";
	private static final String CREATE_TABLE_SCENE_DEVICE = "CREATE TABLE "
			+ TABLE_SCENE_DEVICE + "(" + KEY_SCENE_DEVICE_SCENE_ID
			+ " INTEGER," + KEY_SCENE_DEVICE_DEVICE_ID + " INTEGER,"
			+ KEY_SCENE_DEVICE_DEVICE_STATUS + " INTEGER,"
			+ KEY_SCENE_DEVICE_IS_DELETED + " INTEGER DEFAULT 0" + ")";

	// TABLE ST_WHEN
	private static final String TABLE_ST_WHEN = "st_when";
	private static final String KEY_ST_WHEN_ID = "_st_when_id";
	private static final String KEY_ST_WHEN_DEVICE_ID = "_st_when_device_id";
	private static final String KEY_ST_WHEN_VALUE = "_st_when_value";
	private static final String KEY_ST_WHEN_TYPE = "_st_when_type";
	private static final String KEY_ST_WHEN_IS_DELETED = "_st_when_is_deleted";
	private static final String CREATE_TABLE_ST_WHEN = "CREATE TABLE "
			+ TABLE_ST_WHEN + "(" + KEY_ST_WHEN_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ST_WHEN_DEVICE_ID
			+ " INTEGER," + KEY_ST_WHEN_VALUE + " TEXT," + KEY_ST_WHEN_TYPE
			+ " INTEGER," + KEY_ST_WHEN_IS_DELETED + " INTEGER DEFAULT 0" + ")";

	// TABLE ST_TRIGGER
	private static final String TABLE_ST_TRIGGER = "st_trigger";
	private static final String KEY_ST_TRIGGER_ID = "_st_trigger_id";
	private static final String KEY_ST_TRIGGER_DEVICE_ID = "_st_trigger_device_id";
	private static final String KEY_ST_TRIGGER_VALUE = "_st_trigger_value";
	private static final String KEY_ST_TRIGGER_ACTION = "_st_trigger_action";
	private static final String KEY_ST_TRIGGER_IS_DELETED = "_st_trigger_is_deleted";
	private static final String CREATE_TABLE_ST_TRIGGER = "CREATE TABLE "
			+ TABLE_ST_TRIGGER + "(" + KEY_ST_TRIGGER_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ST_TRIGGER_DEVICE_ID
			+ " INTEGER," + KEY_ST_TRIGGER_VALUE + " TEXT,"
			+ KEY_ST_TRIGGER_ACTION + " INTEGER," + KEY_ST_TRIGGER_IS_DELETED
			+ " INTEGER DEFAULT 0" + ")";

	// TABLE ST_WHEN_TRIGGER
	private static final String TABLE_ST_WHEN_TRIGGER = "st_when_trigger";
	private static final String KEY_ST_WHEN_TRIGGER_WHEN_ID = "_st_when_trigger_when_id";
	private static final String KEY_ST_WHEN_TRIGGER_TRIGGER_ID = "_st_when_trigger_trigger_id";
	private static final String KEY_ST_WHEN_TRIGGER_NAME = "_st_when_trigger_name";
	private static final String KEY_ST_WHEN_TRIGGER_IS_DELETED = "_st_when_trigger_is_deleted";
	private static final String CREATE_TABLE_ST_WHEN_TRIGGER = "CREATE TABLE "
			+ TABLE_ST_WHEN_TRIGGER + "(" + KEY_ST_WHEN_TRIGGER_WHEN_ID
			+ " INTEGER," + KEY_ST_WHEN_TRIGGER_TRIGGER_ID + " INTEGER,"
			+ KEY_ST_WHEN_TRIGGER_NAME + " TEXT,"
			+ KEY_ST_WHEN_TRIGGER_IS_DELETED + " INTEGER DEFAULT 0" + ")";

	// TABLE WIFI
	private static final String TABLE_WIFI = "wifi";
	private static final String KEY_WIFI_ID = "_wifi_id";
	private static final String KEY_WIFI_NAME = "_wifi_name";
	private static final String KEY_WIFI_PASSWORD = "_wifi_password";
	private static final String KEY_WIFI_CODE = "_wifi_code";
	private static final String KEY_WIFI_IS_DELETED = "_wifi_is_deleted";
	private static final String CREATE_TABLE_WIFI = "CREATE TABLE "
			+ TABLE_WIFI + "(" + KEY_WIFI_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_WIFI_NAME + " TEXT,"
			+ KEY_WIFI_PASSWORD + " TEXT," + KEY_WIFI_CODE + " TEXT,"
			+ KEY_WIFI_IS_DELETED + " INTEGER DEFAULT 0" + ")";

	// TABLE BOX
	private static final String TABLE_BOX = "box";
	private static final String KEY_BOX_ID = "_box_id";
	private static final String KEY_BOX_NAME = "_box_name";
	private static final String KEY_BOX_PASSWORD = "_box_password";
	private static final String KEY_BOX_CODE = "_box_code";
	private static final String KEY_BOX_WIFI_ID = "_box_wifi_id";
	private static final String KEY_BOX_IS_DELETED = "_box_is_deleted";
	private static final String CREATE_TABLE_BOX = "CREATE TABLE " + TABLE_BOX
			+ "(" + KEY_BOX_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_BOX_NAME + " TEXT," + KEY_BOX_PASSWORD + " TEXT,"
			+ KEY_BOX_CODE + " TEXT," + KEY_BOX_WIFI_ID + " INTEGER,"
			+ KEY_BOX_IS_DELETED + " INTEGER DEFAULT 0" + ")";

	// TABLE USER
	private static final String TABLE_USER = "user";
	private static final String KEY_USER_ID = "_user_id";
	private static final String KEY_USER_NAME = "_user_name";
	private static final String KEY_USER_EMAIL = "_user_email";
	private static final String KEY_USER_ICON = "_user_icon";
	private static final String KEY_USER_STATUS = "_user_status";
	private static final String KEY_USER_IS_DELETED = "_user_is_deleted";
	private static final String CREATE_TABLE_USER = "CREATE TABLE "
			+ TABLE_USER + "(" + KEY_USER_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_USER_NAME + " TEXT,"
			+ KEY_USER_EMAIL + " TEXT," + KEY_USER_ICON + " TEXT,"
			+ KEY_USER_STATUS + " INTEGER," + KEY_USER_IS_DELETED
			+ " INTEGER DEFAULT 0" + ")";

	// TABLE HISTORY_BOX_WIFI
	private static final String TABLE_HISTORY_BOX_WIFI = "history_box_wifi";
	private static final String KEY_HISTORY_BOX_WIFI_ID = "_history_box_wifi_id";
	private static final String KEY_HISTORY_BOX_WIFI_BOX_ID = "_history_box_wifi_box_id";
	private static final String KEY_HISTORY_BOX_WIFI_WIFI_ID = "_history_box_wifi_wifi_id";
	private static final String KEY_HISTORY_BOX_WIFI_USER_ID = "_history_box_wifi_user_id";
	private static final String KEY_HISTORY_BOX_WIFI_TYPE = "_history_box_wifi_type";
	private static final String KEY_HISTORY_BOX_WIFI_STATUS = "_history_box_wifi_status";
	private static final String KEY_HISTORY_BOX_WIFI_DATETIME = "_history_box_wifi_datetime";
	private static final String CREATE_TABLE_HISTORY_BOX_WIFI = "CREATE TABLE "
			+ TABLE_HISTORY_BOX_WIFI + "(" + KEY_HISTORY_BOX_WIFI_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_BOX_WIFI_BOX_ID + " INTEGER,"
			+ KEY_HISTORY_BOX_WIFI_WIFI_ID + " INTEGER,"
			+ KEY_HISTORY_BOX_WIFI_USER_ID + " INTEGER,"
			+ KEY_HISTORY_BOX_WIFI_TYPE + " INTEGER,"
			+ KEY_HISTORY_BOX_WIFI_STATUS + " TEXT,"
			+ KEY_HISTORY_BOX_WIFI_DATETIME + " TEXT" + ")";

	// TABLE HISTORY_USER_ACTION
	private static final String TABLE_HISTORY_USER_ACTION = "history_user_action";
	private static final String KEY_HISTORY_USER_ACTION_ID = "_history_user_action_id";
	private static final String KEY_HISTORY_USER_ACTION_USER_ID = "_history_user_action_user_id";
	private static final String KEY_HISTORY_USER_ACTION_TYPE = "_history_user_action_type";
	private static final String KEY_HISTORY_USER_ACTION_BY_USER_ID = "_history_user_action_by_user_id";
	private static final String KEY_HISTORY_USER_ACTION_BOX_ID = "_history_user_action_box_id";
	private static final String KEY_HISTORY_USER_ACTION_DATETIME = "_history_user_action_datetime";
	private static final String CREATE_TABLE_HISTORY_USER_ACTION = "CREATE TABLE "
			+ TABLE_HISTORY_USER_ACTION
			+ "("
			+ KEY_HISTORY_USER_ACTION_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_USER_ACTION_USER_ID
			+ " INTEGER,"
			+ KEY_HISTORY_USER_ACTION_TYPE
			+ " INTEGER,"
			+ KEY_HISTORY_USER_ACTION_BY_USER_ID
			+ " INTEGER,"
			+ KEY_HISTORY_USER_ACTION_BOX_ID
			+ " INTEGER,"
			+ KEY_HISTORY_USER_ACTION_DATETIME + " TEXT" + ")";

	// TABLE HISTORY_USER_HOME
	private static final String TABLE_HISTORY_USER_HOME = "history_user_home";
	private static final String KEY_HISTORY_USER_HOME_ID = "_history_user_home_id";
	private static final String KEY_HISTORY_USER_HOME_USER_ID = "_history_user_home_user_id";
	private static final String KEY_HISTORY_USER_HOME_TYPE = "_history_user_home_type";
	private static final String KEY_HISTORY_USER_HOME_DATETIME = "_history_user_home_datetime";
	private static final String CREATE_TABLE_HISTORY_USER_HOME = "CREATE TABLE "
			+ TABLE_HISTORY_USER_HOME
			+ "("
			+ KEY_HISTORY_USER_HOME_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_USER_HOME_USER_ID
			+ " INTEGER,"
			+ KEY_HISTORY_USER_HOME_TYPE
			+ " INTEGER,"
			+ KEY_HISTORY_USER_HOME_DATETIME + " TEXT" + ")";

	// TABLE HISTORY_DEVICE
	private static final String TABLE_HISTORY_DEVICE = "history_device";
	private static final String KEY_HISTORY_DEVICE_ID = "_history_device_id";
	private static final String KEY_HISTORY_DEVICE_TYPE = "_history_device_type";
	private static final String KEY_HISTORY_DEVICE_USER_ID = "_history_device_user_id";
	private static final String KEY_HISTORY_DEVICE_DEVICE_ID = "_history_device_device_id";
	private static final String KEY_HISTORY_DEVICE_SCENE_ID = "_history_device_scene_id";
	private static final String KEY_HISTORY_DEVICE_DATETIME = "_history_device_datetime";
	private static final String CREATE_TABLE_HISTORY_DEVICE = "CREATE TABLE "
			+ TABLE_HISTORY_DEVICE + "(" + KEY_HISTORY_DEVICE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_DEVICE_USER_ID + " INTEGER,"
			+ KEY_HISTORY_DEVICE_DEVICE_ID + " INTEGER,"
			+ KEY_HISTORY_DEVICE_SCENE_ID + " INTEGER,"
			+ KEY_HISTORY_DEVICE_TYPE + " INTEGER,"
			+ KEY_HISTORY_DEVICE_DATETIME + " TEXT" + ")";

	// TABLE HISTORY_PLACE
	private static final String TABLE_HISTORY_PLACE = "history_place";
	private static final String KEY_HISTORY_PLACE_ID = "_history_place_id";
	private static final String KEY_HISTORY_PLACE_PLACE_ID = "_history_place_place_id";
	private static final String KEY_HISTORY_PLACE_TYPE = "_history_place_type";
	private static final String KEY_HISTORY_PLACE_USER_ID = "_history_place_user_id";
	private static final String KEY_HISTORY_PLACE_DATETIME = "_history_place_datetime";
	private static final String CREATE_TABLE_HISTORY_PLACE = "CREATE TABLE "
			+ TABLE_HISTORY_PLACE + "(" + KEY_HISTORY_PLACE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_PLACE_PLACE_ID + " INTEGER," + KEY_HISTORY_PLACE_TYPE
			+ " INTEGER," + KEY_HISTORY_PLACE_USER_ID + " INTEGER,"
			+ KEY_HISTORY_PLACE_DATETIME + " TEXT" + ")";

	// TABLE HISTORY_SCENE
	private static final String TABLE_HISTORY_SCENE = "history_scene";
	private static final String KEY_HISTORY_SCENE_ID = "_history_scene_id";
	private static final String KEY_HISTORY_SCENE_SCENE_ID = "_history_scene_scene_id";
	private static final String KEY_HISTORY_SCENE_TYPE = "_history_scene_type";
	private static final String KEY_HISTORY_SCENE_USER_ID = "_history_scene_user_id";
	private static final String KEY_HISTORY_SCENE_DATETIME = "_history_scene_datetime";
	private static final String CREATE_TABLE_HISTORY_SCENE = "CREATE TABLE "
			+ TABLE_HISTORY_SCENE + "(" + KEY_HISTORY_SCENE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_SCENE_SCENE_ID + " INTEGER," + KEY_HISTORY_SCENE_TYPE
			+ " INTEGER," + KEY_HISTORY_SCENE_USER_ID + " INTEGER,"
			+ KEY_HISTORY_SCENE_DATETIME + " TEXT" + ")";

	// TABLE HISTORY_SMART_TRIGGER
	private static final String TABLE_HISTORY_SMART_TRIGGER = "history_smart_trigger";
	private static final String KEY_HISTORY_SMART_TRIGGER_ID = "_history_smart_trigger_id";
	private static final String KEY_HISTORY_SMART_TRIGGER_WHEN_TRIGGER_ID = "_history_smart_trigger_when_trigger_id";
	private static final String KEY_HISTORY_SMART_TRIGGER_TYPE = "_history_smart_trigger_type";
	private static final String KEY_HISTORY_SMART_TRIGGER_USER_ID = "_history_smart_trigger_user_id";
	private static final String KEY_HISTORY_SMART_TRIGGER_DATETIME = "_history_smart_trigger_datetime";
	private static final String CREATE_TABLE_HISTORY_SMART_TRIGGER = "CREATE TABLE "
			+ TABLE_HISTORY_SMART_TRIGGER
			+ "("
			+ KEY_HISTORY_SMART_TRIGGER_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_SMART_TRIGGER_WHEN_TRIGGER_ID
			+ " INTEGER,"
			+ KEY_HISTORY_SMART_TRIGGER_TYPE
			+ " INTEGER,"
			+ KEY_HISTORY_SMART_TRIGGER_USER_ID
			+ " INTEGER,"
			+ KEY_HISTORY_SMART_TRIGGER_DATETIME + " TEXT" + ")";

	// TABLE HISTORY_WARNING
	private static final String TABLE_HISTORY_WARNING = "history_warning";
	private static final String KEY_HISTORY_WARNING_ID = "_history_warning_id";
	private static final String KEY_HISTORY_WARNING_DEVICE_ID = "_history_warning_device_id";
	private static final String KEY_HISTORY_WARNING_DATETIME = "_history_warning_datetime";
	private static final String CREATE_TABLE_HISTORY_WARNING = "CREATE TABLE "
			+ TABLE_HISTORY_WARNING + "(" + KEY_HISTORY_WARNING_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_WARNING_DEVICE_ID + " INTEGER,"
			+ KEY_HISTORY_WARNING_DATETIME + " TEXT" + ")";

	// TABLE HISTORY_MOVE_PLACE_OF_DEVICE
	private static final String TABLE_HISTORY_MOVE_PLACE_OF_DEVICE = "history_move_place_of_device";
	private static final String KEY_HISTORY_MOVE_PLACE_OF_DEVICE_ID = "_history_move_place_of_device_id";
	private static final String KEY_HISTORY_MOVE_PLACE_OF_DEVICE_DEVICE_ID = "_history_move_place_of_device_device_id";
	private static final String KEY_HISTORY_MOVE_PLACE_OF_DEVICE_PLACE_ID = "_history_move_place_of_device_place_id";
	private static final String KEY_HISTORY_MOVE_PLACE_OF_DEVICE_USER_ID = "_history_move_place_of_device_user_id";
	private static final String KEY_HISTORY_MOVE_PLACE_OF_DEVICE_DATETIME = "_history_move_place_of_device_datetime";
	private static final String CREATE_TABLE_HISTORY_MOVE_PLACE_OF_DEVICE = "CREATE TABLE "
			+ TABLE_HISTORY_MOVE_PLACE_OF_DEVICE
			+ "("
			+ KEY_HISTORY_MOVE_PLACE_OF_DEVICE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_MOVE_PLACE_OF_DEVICE_DEVICE_ID
			+ " INTEGER,"
			+ KEY_HISTORY_MOVE_PLACE_OF_DEVICE_PLACE_ID
			+ " INTEGER,"
			+ KEY_HISTORY_MOVE_PLACE_OF_DEVICE_USER_ID
			+ " INTEGER,"
			+ KEY_HISTORY_MOVE_PLACE_OF_DEVICE_DATETIME + " TEXT" + ")";

	// TABLE s
	private static final String TABLE_HISTORY_NAME_CAPABILITY_OF_PLACE = "history_name_capability_of_place";
	private static final String KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_ID = "_history_name_capability_of_place_id";
	private static final String KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_PLACE_ID = "_history_name_capability_of_place_place_id";
	private static final String KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_USER_ID = "_history_name_capability_of_place_user_id";
	private static final String KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_RENAME_FROM = "_history_name_capability_of_place_rename_from";
	private static final String KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_RENAME_TO = "_history_name_capability_of_place_rename_to";
	private static final String KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_CAPABILITY_FROM = "_history_name_capability_of_place_capability_from";
	private static final String KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_CAPABILITY_TO = "_history_name_capability_of_place_capability_to";
	private static final String KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_DATETIME = "_history_name_capability_of_place_datetime";
	private static final String CREATE_TABLE_HISTORY_NAME_CAPABILITY_OF_PLACE = "CREATE TABLE "
			+ TABLE_HISTORY_NAME_CAPABILITY_OF_PLACE
			+ "("
			+ KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_PLACE_ID
			+ " INTEGER,"
			+ KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_USER_ID
			+ " INTEGER,"
			+ KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_RENAME_FROM
			+ " TEXT,"
			+ KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_RENAME_TO
			+ " TEXT,"
			+ KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_CAPABILITY_FROM
			+ " INTEGER,"
			+ KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_CAPABILITY_TO
			+ " INTEGER,"
			+ KEY_HISTORY_NAME_CAPABILITY_OF_PLACE_DATETIME
			+ " TEXT" + ")";

	public SQLiteAsusNewHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(CREATE_TABLE_DEVICE);
		database.execSQL(CREATE_TABLE_PLACE);
		database.execSQL(CREATE_TABLE_SCENE);
		database.execSQL(CREATE_TABLE_SCENE_DEVICE);
		database.execSQL(CREATE_TABLE_ST_WHEN);
		database.execSQL(CREATE_TABLE_ST_TRIGGER);
		database.execSQL(CREATE_TABLE_ST_WHEN_TRIGGER);
		database.execSQL(CREATE_TABLE_WIFI);
		database.execSQL(CREATE_TABLE_BOX);
		database.execSQL(CREATE_TABLE_USER);
		database.execSQL(CREATE_TABLE_HISTORY_BOX_WIFI);
		database.execSQL(CREATE_TABLE_HISTORY_USER_ACTION);
		database.execSQL(CREATE_TABLE_HISTORY_USER_HOME);
		database.execSQL(CREATE_TABLE_HISTORY_DEVICE);
		database.execSQL(CREATE_TABLE_HISTORY_PLACE);
		database.execSQL(CREATE_TABLE_HISTORY_SCENE);
		database.execSQL(CREATE_TABLE_HISTORY_SMART_TRIGGER);
		database.execSQL(CREATE_TABLE_HISTORY_WARNING);
		database.execSQL(CREATE_TABLE_HISTORY_MOVE_PLACE_OF_DEVICE);
		database.execSQL(CREATE_TABLE_HISTORY_NAME_CAPABILITY_OF_PLACE);

		// add default record for PLACE UNCATEGORIZED
		database.execSQL("INSERT INTO " + TABLE_PLACE + "(" + KEY_PLACE_ID
				+ "," + KEY_PLACE_NAME + "," + KEY_PLACE_ICON + ","
				+ KEY_PLACE_CAPABILITY_TYPE + "," + KEY_PLACE_IS_DELETED
				+ ") values(1, \"Uncategorized\", \"\", 0, 0)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_DEVICE);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_PLACE);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_SCENE);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_SCENE_DEVICE);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_ST_WHEN);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_ST_TRIGGER);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_ST_WHEN_TRIGGER);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_WIFI);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_BOX);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_USER);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_HISTORY_BOX_WIFI);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_HISTORY_USER_ACTION);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_HISTORY_USER_HOME);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_HISTORY_DEVICE);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_HISTORY_PLACE);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_HISTORY_SCENE);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_HISTORY_SMART_TRIGGER);
		db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_HISTORY_WARNING);
		db.execSQL("DROP TABLE IF EXISTS "
				+ CREATE_TABLE_HISTORY_MOVE_PLACE_OF_DEVICE);
		db.execSQL("DROP TABLE IF EXISTS "
				+ CREATE_TABLE_HISTORY_NAME_CAPABILITY_OF_PLACE);
		onCreate(db);
	}

	/*
	 * ADD NEW DEVICE CARD
	 */
	public int addDevice(DeviceItem item) {
		SQLiteDatabase db = this.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put(KEY_DEVICE_NAME, item.getDeviceName());
		values.put(KEY_DEVICE_ICON, item.getDeviceIcon());
		values.put(KEY_DEVICE_STATUS, item.getDeviceStatus());
		values.put(KEY_DEVICE_TYPE, item.getDeviceType());
		values.put(KEY_DEVICE_CONTROL_VALUE, item.getStatusValue());
		values.put(KEY_DEVICE_CODE, item.getDeviceCode());
		values.put(KEY_DEVICE_BOX_ID, item.getDeviceBoxId());
		values.put(KEY_DEVICE_PLACE_ID, item.getDevicePlaceId());
		values.put(KEY_DEVICE_USER_ID, item.getUserId());
		values.put(KEY_DEVICE_FAVORITE, item.getFavorite());
		values.put(KEY_DEVICE_IS_DELETED, 0);

		int id = (int) db.insert(TABLE_DEVICE, null, values);
		if (db != null) {
			db.close();
		}
		return id;
	}

	public void updateDeviceTop(DeviceItem deviceItem) {
		SQLiteDatabase db = this.getReadableDatabase();

		try {

			ContentValues values = new ContentValues();
			/*
			 * values.put(KEY_DEVICE_NAME, deviceItem.getDeviceName());
			 * values.put(KEY_DEVICE_ICON, deviceItem.getDeviceIcon());
			 * values.put(KEY_DEVICE_STATUS, deviceItem.getDeviceStatus());
			 * values.put(KEY_DEVICE_TYPE, deviceItem.getDeviceType());
			 * values.put(KEY_DEVICE_CONTROL_VALUE,
			 * deviceItem.getStatusValue()); values.put(KEY_DEVICE_CODE,
			 * deviceItem.getDeviceCode()); values.put(KEY_DEVICE_BOX_ID,
			 * deviceItem.getDeviceBoxId()); values.put(KEY_DEVICE_PLACE_ID,
			 * deviceItem.getDevicePlaceId()); values.put(KEY_DEVICE_USER_ID,
			 * deviceItem.getUserId()); values.put(KEY_DEVICE_IS_DELETED, 0);
			 */
			values.put(KEY_DEVICE_FAVORITE, 0);
			db.update(TABLE_DEVICE, values, KEY_DEVICE_ID + " = ? ",
					new String[] { String.valueOf(deviceItem.getDeviceId()) });

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (db != null) {
			db.close();
		}

	}

	public void updateDevice(int id, int status, int favorite, int placeId,
			String nameDevice, String photoDevice, int deleted) {

		SQLiteDatabase db = this.getReadableDatabase();
		ContentValues values = new ContentValues();

		if (favorite != -1) {
			try {

				values.put(KEY_DEVICE_FAVORITE, favorite);
				db.update(TABLE_DEVICE, values, KEY_DEVICE_ID + " = ? ",
						new String[] { String.valueOf(id) });

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		if (status != -1) {
			try {

				values.put(KEY_DEVICE_STATUS, status);
				db.update(TABLE_DEVICE, values, KEY_DEVICE_ID + " = ? ",
						new String[] { String.valueOf(id) });

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		if (placeId != -1) {
			try {

				values.put(KEY_DEVICE_PLACE_ID, placeId);
				db.update(TABLE_DEVICE, values, KEY_DEVICE_ID + " = ? ",
						new String[] { String.valueOf(id) });

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		if (nameDevice != "-1") {
			try {

				values.put(KEY_DEVICE_NAME, nameDevice);
				db.update(TABLE_DEVICE, values, KEY_DEVICE_ID + " = ? ",
						new String[] { String.valueOf(id) });

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		if (deleted != -1) {
			try {

				values.put(KEY_DEVICE_IS_DELETED, deleted);
				db.update(TABLE_DEVICE, values, KEY_DEVICE_ID + " = ? ",
						new String[] { String.valueOf(id) });
				return;
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		if (photoDevice != "-1") {
			try {

				values.put(KEY_DEVICE_ICON, photoDevice);
				db.update(TABLE_DEVICE, values, KEY_DEVICE_ID + " = ? ",
						new String[] { String.valueOf(id) });

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		if (db != null) {
			db.close();
		}

	}

	// RENAME DEVICE NAMNT16
	public int renameDevice(DeviceItem item) {
		SQLiteDatabase db = this.getReadableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_DEVICE_NAME, item.getDeviceName());

		int id = db.update(TABLE_DEVICE, values,
				KEY_DEVICE_ID + "=" + String.valueOf(item.getDeviceId()), null);
		if (db != null) {

			db.close();
		}
		return id;
	}

	// REMOVE DEVICE NAMNT16
	public int removeDevice(DeviceItem item) {
		SQLiteDatabase db = this.getReadableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_DEVICE_IS_DELETED, 1);
		int id = db.update(TABLE_DEVICE, values,
				KEY_DEVICE_ID + "=" + String.valueOf(item.getDeviceId()), null);
		if (db != null) {

			db.close();
		}
		return id;
	}

	// Edit place DEVICE NAMNT16
	public int editPlaceDevice(DeviceItem item) {
		SQLiteDatabase db = this.getReadableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_DEVICE_PLACE_ID, item.getDevicePlaceId());
		int id = db.update(TABLE_DEVICE, values,
				KEY_DEVICE_ID + "=" + String.valueOf(item.getDevicePlaceId()),
				null);
		if (db != null) {

			db.close();
		}
		return id;
	}

	// get place id NamNT16
	public int getPlaceId(PlaceItem item) {
		String selectQuery = "SELECT " + KEY_PLACE_ID + " FROM " + TABLE_PLACE
				+ " WHERE " + KEY_PLACE_NAME + "='" + item.getPlaceName() + "'";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursorSqliteAsusHelper = db.rawQuery(selectQuery, null);
		if (cursorSqliteAsusHelper.getCount() > 0) {
			if (cursorSqliteAsusHelper.moveToFirst()) {
				return Integer.parseInt(cursorSqliteAsusHelper.getString(0));
			}
		}
		if (db != null) {

			db.close();
		}
		return 0;
	}

	public ArrayList<DeviceItem> getListDeviceTrigg() {
		ArrayList<DeviceItem> list = new ArrayList<DeviceItem>();
		String selectQuery = "SELECT * from Device";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		try {
			if (cursor.moveToFirst()) {
				do {
					DeviceItem device = new DeviceItem();
					device.setDeviceId(Integer.parseInt(cursor.getString(0)));
					device.setDeviceName(cursor.getString(1));
					device.setDeviceIcon(cursor.getString(2));
					device.setDeviceType(Integer.parseInt(cursor.getString(4)));
					// device.setStatusValue(Integer.parseInt(cursor.getString(4)));
					// device.setDeviceStatus(Integer.parseInt(cursor.getString(5)));

					list.add(device);
				} while (cursor.moveToNext());
			}
			return list;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	/*
	 * GET LIST DEVICE BY GROUP TYPE (ON/OFF || STATUS || WARNING)
	 */
	public ArrayList<DeviceItem> getListDevice(int deviceGroupType) {
		ArrayList<DeviceItem> list = new ArrayList<DeviceItem>();
		String selectQuery = "SELECT " + TABLE_DEVICE + "." + KEY_DEVICE_ID
				+ ", " + TABLE_DEVICE + "." + KEY_DEVICE_NAME + ", "
				+ TABLE_DEVICE + "." + KEY_DEVICE_ICON + ", " + TABLE_DEVICE
				+ "." + KEY_DEVICE_TYPE + ", " + TABLE_DEVICE + "."
				+ KEY_DEVICE_CONTROL_VALUE + ", " + TABLE_DEVICE + "."
				+ KEY_DEVICE_STATUS + ", "

				+ TABLE_DEVICE + "." + KEY_DEVICE_USER_ID + ", " + TABLE_USER
				+ "." + KEY_USER_ICON + ", " + TABLE_USER + "."
				+ KEY_USER_STATUS + ", "

				+ TABLE_DEVICE + "." + KEY_DEVICE_PLACE_ID + ", " + TABLE_PLACE
				+ "." + KEY_PLACE_NAME

				+ " FROM " + TABLE_DEVICE

				+ " LEFT OUTER JOIN " + TABLE_PLACE + " ON " + TABLE_PLACE
				+ "." + KEY_PLACE_ID + " = " + TABLE_DEVICE + "."
				+ KEY_DEVICE_PLACE_ID

				+ " LEFT OUTER JOIN " + TABLE_USER + " ON " + TABLE_USER + "."
				+ KEY_USER_ID + " = " + TABLE_DEVICE + "." + KEY_DEVICE_USER_ID;

		ArrayList<Integer> listType = DeviceHelper.getListType(deviceGroupType);
		String where = TextUtils.join(",", listType);
		where = "(" + where + ")";
		selectQuery += " WHERE " + TABLE_DEVICE + "." + KEY_DEVICE_TYPE
				+ " IN " + where;

		selectQuery += " AND " + TABLE_DEVICE + "." + KEY_DEVICE_IS_DELETED
				+ " = 0 ";

		selectQuery += " ORDER BY " + TABLE_DEVICE + "." + KEY_DEVICE_ID
				+ " DESC";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		try {
			if (cursor.moveToFirst()) {
				do {
					DeviceItem device = new DeviceItem();
					device.setDeviceId(Integer.parseInt(cursor.getString(0)));
					device.setDeviceName(cursor.getString(1));
					device.setDeviceIcon(cursor.getString(2));
					device.setDeviceType(Integer.parseInt(cursor.getString(3)));
					device.setStatusValue(Integer.parseInt(cursor.getString(4)));
					device.setDeviceStatus(Integer.parseInt(cursor.getString(5)));
					int userId = Integer.parseInt(cursor.getString(6));
					if (userId > 0) {
						device.setUserId(userId);
						device.setShowUserStatus(1);
						device.setUserIcon(cursor.getString(7));
						device.setUserStatus(Integer.parseInt(cursor
								.getString(8)));
					} else {
						device.setUserId(0);
						device.setShowUserStatus(0);
						device.setUserIcon("");
						device.setUserStatus(0);
					}

					device.setDevicePlaceId(Integer.parseInt(cursor
							.getString(9)));
					device.setDevicePlace(cursor.getString(10));
					list.add(device);
				} while (cursor.moveToNext());
			}
			return list;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	public ArrayList<DeviceItem> getListDeviceInPlace(int placeId) {
		ArrayList<DeviceItem> list = new ArrayList<DeviceItem>();
		String selectQuery = "SELECT " + TABLE_DEVICE + "." + KEY_DEVICE_ID
				+ ", " + TABLE_DEVICE + "." + KEY_DEVICE_NAME + ", "
				+ TABLE_DEVICE + "." + KEY_DEVICE_ICON + ", " + TABLE_DEVICE
				+ "." + KEY_DEVICE_TYPE + ", " + TABLE_DEVICE + "."
				+ KEY_DEVICE_CONTROL_VALUE + ", " + TABLE_DEVICE + "."
				+ KEY_DEVICE_STATUS + ", "

				+ TABLE_DEVICE + "." + KEY_DEVICE_USER_ID + " FROM "
				+ TABLE_DEVICE

				+ " where " + TABLE_DEVICE + "." + KEY_DEVICE_PLACE_ID + " = "
				+ placeId;

		selectQuery += " ORDER BY " + TABLE_DEVICE + "." + KEY_DEVICE_ID
				+ " DESC";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		try {
			if (cursor.moveToFirst()) {
				do {
					DeviceItem device = new DeviceItem();
					device.setDeviceId(Integer.parseInt(cursor.getString(0)));
					device.setDeviceName(cursor.getString(1));
					device.setDeviceIcon(cursor.getString(2));
					device.setDeviceType(Integer.parseInt(cursor.getString(3)));
					device.setStatusValue(Integer.parseInt(cursor.getString(4)));
					device.setDeviceStatus(Integer.parseInt(cursor.getString(5)));

					list.add(device);
				} while (cursor.moveToNext());
			}
			return list;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	public ArrayList<DeviceItem> getListDeviceAll() {
		ArrayList<DeviceItem> list = new ArrayList<DeviceItem>();
		String selectQuery = "SELECT " + TABLE_DEVICE + "." + KEY_DEVICE_ID
				+ ", " + TABLE_DEVICE + "." + KEY_DEVICE_NAME + ", "
				+ TABLE_DEVICE + "." + KEY_DEVICE_ICON + ", " + TABLE_DEVICE
				+ "." + KEY_DEVICE_TYPE + ", " + TABLE_DEVICE + "."
				+ KEY_DEVICE_CONTROL_VALUE + ", " + TABLE_DEVICE + "."
				+ KEY_DEVICE_STATUS + ", "

				+ TABLE_DEVICE + "." + KEY_DEVICE_USER_ID + " , "
				+ TABLE_DEVICE + "." + KEY_DEVICE_FAVORITE + " , "
				+ TABLE_PLACE + "." + KEY_PLACE_NAME + " FROM " + TABLE_DEVICE
				+ "," + TABLE_PLACE + " where  " + TABLE_PLACE + "."
				+ KEY_PLACE_ID + " = " + TABLE_DEVICE + "."
				+ KEY_DEVICE_PLACE_ID + " AND " + TABLE_DEVICE + "."
				+ KEY_DEVICE_IS_DELETED + " =0 ";

		selectQuery += " ORDER BY " + TABLE_DEVICE + "." + KEY_DEVICE_FAVORITE
				+ " DESC";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		try {
			if (cursor.moveToFirst()) {
				do {
					DeviceItem device = new DeviceItem();
					device.setDeviceId(Integer.parseInt(cursor.getString(0)));
					device.setDeviceName(cursor.getString(1));
					device.setDeviceIcon(cursor.getString(2));
					device.setDeviceType(Integer.parseInt(cursor.getString(3)));
					device.setStatusValue(Integer.parseInt(cursor.getString(4)));
					device.setDeviceStatus(Integer.parseInt(cursor.getString(5)));
					device.setUserId(Integer.parseInt(cursor.getString(6)));
				//	device.setFavorite(Integer.parseInt(cursor.getString(7)));
					device.setDevicePlace(cursor.getString(8));
					list.add(device);
				} while (cursor.moveToNext());
			}
			return list;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	public ArrayList<DeviceItem> getListDeviceFavorite() {
		ArrayList<DeviceItem> list = new ArrayList<DeviceItem>();
		String selectQuery = "SELECT " + TABLE_DEVICE + "." + KEY_DEVICE_ID
				+ ", " + TABLE_DEVICE + "." + KEY_DEVICE_NAME + ", "
				+ TABLE_DEVICE + "." + KEY_DEVICE_ICON + ", " + TABLE_DEVICE
				+ "." + KEY_DEVICE_TYPE + ", " + TABLE_DEVICE + "."
				+ KEY_DEVICE_CONTROL_VALUE + ", " + TABLE_DEVICE + "."
				+ KEY_DEVICE_STATUS + ", "

				+ TABLE_DEVICE + "." + KEY_DEVICE_USER_ID + " , "
				+ TABLE_DEVICE + "." + KEY_DEVICE_FAVORITE + " , "
				+ TABLE_PLACE + "." + KEY_PLACE_NAME + " FROM " + TABLE_DEVICE
				+ "," + TABLE_PLACE + " where  " + TABLE_PLACE + "."
				+ KEY_PLACE_ID + " = " + TABLE_DEVICE + "."
				+ KEY_DEVICE_PLACE_ID + " AND " + TABLE_DEVICE + "."
				+ KEY_DEVICE_FAVORITE + " >0 ";

		selectQuery += " ORDER BY " + TABLE_DEVICE + "." + KEY_DEVICE_FAVORITE
				+ " ASC";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		try {
			if (cursor.moveToFirst()) {
				do {
					DeviceItem device = new DeviceItem();
					device.setDeviceId(Integer.parseInt(cursor.getString(0)));
					device.setDeviceName(cursor.getString(1));
					device.setDeviceIcon(cursor.getString(2));
					device.setDeviceType(Integer.parseInt(cursor.getString(3)));
					device.setStatusValue(Integer.parseInt(cursor.getString(4)));
					device.setDeviceStatus(Integer.parseInt(cursor.getString(5)));
					device.setUserId(Integer.parseInt(cursor.getString(6)));
					device.setFavorite(Integer.parseInt(cursor.getString(7)));
					device.setDevicePlace(cursor.getString(8));
					if (list.size() == 4)
						return list;
					list.add(device);

				} while (cursor.moveToNext());
			}
			return list;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	/*
	 * Check exist place name
	 */
	public boolean checkExistPlace(PlaceItem item) {
		String selectQuery = "SELECT " + KEY_PLACE_ID + " FROM " + TABLE_PLACE
				+ " WHERE " + KEY_PLACE_NAME + "='" + item.getPlaceName() + "'";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursorSqliteAsusHelper = db.rawQuery(selectQuery, null);
		if (cursorSqliteAsusHelper.getCount() > 0) {
			return true;
		}
		if (db != null) {

			db.close();
		}
		return false;
	}

	/*
	 * Add new Place
	 */
	public int addPlace(PlaceItem item) {
		int id = -1;
		SQLiteDatabase db = this.getReadableDatabase();
		if (db != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_PLACE_NAME, item.getPlaceName());
			values.put(KEY_PLACE_ICON, item.getPlaceImage());
			values.put(KEY_PLACE_CAPABILITY_TYPE, item.getCapabilityType());
			values.put(KEY_PLACE_IS_DELETED, 0);
			try {
				id = (int) db.insert(TABLE_PLACE, null, values);
			} catch (Exception e) {
				id = -1;
			}
			db.close();
		}
		return id;
	}

	public int addHistoryPlace(HistoryPlaceItem item) {
		int id = -1;
		SQLiteDatabase db = this.getReadableDatabase();
		if (db != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_HISTORY_PLACE_PLACE_ID,
					item.getHistoryPlacePlaceId());
			values.put(KEY_HISTORY_PLACE_TYPE, item.getHistoryPlaceType());
			values.put(KEY_HISTORY_PLACE_USER_ID, item.getHistoryPlaceUserId());
			values.put(KEY_HISTORY_PLACE_DATETIME, item.getHistoryPlaceDate());
			try {
				id = (int) db.insert(TABLE_HISTORY_PLACE, null, values);
			} catch (Exception e) {
				id = -1;
			}
			db.close();
		}
		return id;
	}

	/*
	 * Get list all place
	 */
	public ArrayList<PlaceItem> getListPlace() {
		ArrayList<PlaceItem> lstPlace = new ArrayList<PlaceItem>();
		String selectQuery = "SELECT " + TABLE_PLACE + "." + KEY_PLACE_ID
				+ ", " + TABLE_PLACE + "." + KEY_PLACE_NAME + ", "
				+ TABLE_PLACE + "." + KEY_PLACE_ICON + ", " + TABLE_PLACE + "."
				+ KEY_PLACE_CAPABILITY_TYPE + "," + "COUNT (" + TABLE_DEVICE
				+ "." + KEY_DEVICE_ID + ") as DEVICE_NUM" + " FROM "
				+ TABLE_PLACE

				+ " LEFT OUTER JOIN " + TABLE_DEVICE + " ON " + TABLE_DEVICE
				+ "." + KEY_DEVICE_PLACE_ID + " = " + TABLE_PLACE + "."
				+ KEY_PLACE_ID;

		selectQuery += " WHERE " + TABLE_PLACE + "." + KEY_PLACE_IS_DELETED
				+ " = 0 ";

		selectQuery += " GROUP BY " + TABLE_PLACE + "." + KEY_PLACE_ID;
		selectQuery += " ORDER BY " + TABLE_PLACE + "." + KEY_PLACE_ID
				+ " DESC";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursorSqliteAsusHelper = db.rawQuery(selectQuery, null);
		try {
			if (cursorSqliteAsusHelper.moveToFirst()) {
				do {
					PlaceItem place = new PlaceItem();
					place.setPlaceId(Integer.parseInt(cursorSqliteAsusHelper
							.getString(0)));
					place.setPlaceName(cursorSqliteAsusHelper.getString(1));
					place.setPlaceImage(cursorSqliteAsusHelper.getString(2));
					place.setCapabilityType(Integer
							.parseInt(cursorSqliteAsusHelper.getString(3)));
					place.setDeviceNumber(Integer
							.parseInt(cursorSqliteAsusHelper.getString(4)));
					lstPlace.add(place);
				} while (cursorSqliteAsusHelper.moveToNext());
			}
			return lstPlace;
		} finally {
			if (cursorSqliteAsusHelper != null) {
				cursorSqliteAsusHelper.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	public PlaceItem getInfoOfPlace(int placeID) {
		PlaceItem place = new PlaceItem();
		String selectQuery = "SELECT " + TABLE_PLACE + "." + KEY_PLACE_ID
				+ ", " + TABLE_PLACE + "." + KEY_PLACE_NAME + ", "
				+ TABLE_PLACE + "." + KEY_PLACE_ICON + ", " + TABLE_PLACE + "."
				+ KEY_PLACE_CAPABILITY_TYPE + "," + "COUNT (" + TABLE_DEVICE
				+ "." + KEY_DEVICE_ID + ") as DEVICE_NUM" + ","
				+ TABLE_HISTORY_PLACE + "." + KEY_HISTORY_PLACE_DATETIME;
		selectQuery += " FROM " + TABLE_PLACE + " LEFT OUTER JOIN "
				+ TABLE_DEVICE + " ON " + TABLE_DEVICE + "."
				+ KEY_DEVICE_PLACE_ID + " = " + TABLE_PLACE + "."
				+ KEY_PLACE_ID + " LEFT OUTER JOIN " + TABLE_HISTORY_PLACE
				+ " ON " + TABLE_HISTORY_PLACE + "."
				+ KEY_HISTORY_PLACE_PLACE_ID + " = " + TABLE_PLACE + "."
				+ KEY_PLACE_ID;
		selectQuery += " WHERE " + TABLE_PLACE + "." + KEY_PLACE_ID + " = "
				+ placeID;
		selectQuery += " ORDER BY " + TABLE_PLACE + "." + KEY_PLACE_ID
				+ " DESC";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursorSqliteAsusHelper = db.rawQuery(selectQuery, null);
		try {
			if (cursorSqliteAsusHelper.moveToFirst()) {
				do {

					place.setPlaceId(Integer.parseInt(cursorSqliteAsusHelper
							.getString(0)));
					place.setPlaceName(cursorSqliteAsusHelper.getString(1));
					place.setPlaceImage(cursorSqliteAsusHelper.getString(2));
					place.setCapabilityType(Integer
							.parseInt(cursorSqliteAsusHelper.getString(3)));
					place.setDeviceNumber(Integer
							.parseInt(cursorSqliteAsusHelper.getString(4)));
					place.setDateCreated(cursorSqliteAsusHelper.getString(5));
				} while (cursorSqliteAsusHelper.moveToNext());
			}
			return place;
		} finally {
			if (cursorSqliteAsusHelper != null) {
				cursorSqliteAsusHelper.close();
			}
			if (db != null) {
				db.close();
			}
		}
	}

	// record PlaceItem
	public void insert_data(PlaceItem item) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(KEY_PLACE_ID, item.getPlaceId());
		values.put(KEY_PLACE_NAME, item.getPlaceName());
		values.put(KEY_PLACE_ICON, item.getPlaceImage());
		values.put(KEY_PLACE_CAPABILITY_TYPE, item.getCapabilityType());
		// db.insert(CREATE_TABLE_PLACE, null, values);
		db.insert(TABLE_PLACE, null, values);
		db.close();

	}

	/*
	 * GET LIST DEVICE FROM PLACE_ID
	 */
	public ArrayList<DeviceItem> getAllDevice(int placeId) {
		ArrayList<DeviceItem> listDevice = new ArrayList<DeviceItem>();

		String sql = "SELECT " + TABLE_DEVICE + "." + KEY_DEVICE_ID + ","
				+ TABLE_DEVICE + "." + KEY_DEVICE_NAME + "," + TABLE_DEVICE
				+ "." + KEY_DEVICE_ICON + "," + TABLE_DEVICE + "."
				+ KEY_DEVICE_STATUS + "," + TABLE_DEVICE + "."
				+ KEY_DEVICE_TYPE + "," + TABLE_DEVICE + "."
				+ KEY_DEVICE_CONTROL_VALUE + "," + TABLE_DEVICE + "."
				+ KEY_DEVICE_CODE + "," + TABLE_DEVICE + "."
				+ KEY_DEVICE_BOX_ID + "," + TABLE_DEVICE + "."
				+ KEY_DEVICE_PLACE_ID + "," + TABLE_DEVICE + "."
				+ KEY_DEVICE_USER_ID + "," + TABLE_PLACE + "." + KEY_PLACE_NAME
				+ " FROM " + TABLE_DEVICE + " INNER JOIN " + TABLE_PLACE
				+ " ON " + TABLE_DEVICE + "." + KEY_DEVICE_PLACE_ID + " = "
				+ TABLE_PLACE + "." + KEY_PLACE_ID;

		sql += " WHERE " + KEY_PLACE_ID + "=" + placeId;

		/*
		 * String selectQuery = "SELECT * FROM " + TABLE_DEVICE + " WHERE " +
		 * KEY_DEVICE_PLACE_ID + "=" + placeId; String selectQuery2 = "SELECT "
		 * + KEY_PLACE_NAME + " FROM " + TABLE_PLACE + " WHERE " + KEY_PLACE_ID
		 * + "=" + placeId;
		 */
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor curso = db.rawQuery(sql, null);
		// Cursor curso2 = db.rawQuery(selectQuery2, null);
		try {
			if (curso.moveToFirst() /* && curso2.moveToFirst() */) {
				do {
					DeviceItem de = new DeviceItem();

					de.setDeviceId(Integer.parseInt(curso.getString(0)
							.toString()));
					de.setDeviceName(curso.getString(1));
					de.setDeviceIcon(curso.getString(2));
					de.setDeviceStatus(Integer.parseInt(curso.getString(3)
							.toString()));
					de.setDeviceType(Integer.parseInt(curso.getString(4)
							.toString()));
					de.setDeviceStatus(Integer.parseInt(curso.getString(5)
							.toString()));
					de.setDeviceCode(curso.getString(6));
					de.setDeviceBoxId(Integer.parseInt(curso.getString(7)
							.toString()));
					de.setDevicePlaceId(Integer.parseInt(curso.getString(8)
							.toString()));
					de.setUserId(Integer
							.parseInt(curso.getString(9).toString()));
					de.setDevicePlace(curso.getString(10));
					listDevice.add(de);

				} while (curso.moveToNext());

			}

		} catch (Exception e) {

		}

		return listDevice;
	}

	public ArrayList<SceneItem> getSceneByPlaceId(int placeId) {
		ArrayList<SceneItem> listScene = new ArrayList<SceneItem>();
		String selectQuery = "SELECT " + TABLE_SCENE + "." + KEY_SCENE_ID
				+ ", " + TABLE_SCENE + "." + KEY_SCENE_NAME + ", "
				+ TABLE_SCENE + "." + KEY_SCENE_ICON + ", " + TABLE_SCENE + "."
				+ KEY_SCENE_COLOR + ", " + TABLE_SCENE + "."
				+ KEY_SCENE_PLACE_ID + ", " + TABLE_PLACE + "."
				+ KEY_PLACE_NAME;
		selectQuery += " FROM " + TABLE_SCENE + " LEFT OUTER JOIN "
				+ TABLE_PLACE + " ON " + TABLE_SCENE + "." + KEY_SCENE_PLACE_ID
				+ " = " + TABLE_PLACE + "." + KEY_PLACE_ID;
		selectQuery += " WHERE " + TABLE_SCENE + "." + KEY_SCENE_PLACE_ID + "="
				+ placeId;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor curso = db.rawQuery(selectQuery, null);
		try {
			if (curso.moveToFirst()) {
				do {

					SceneItem itemScene = new SceneItem();
					itemScene.setSceneId(Integer.parseInt(curso.getString(0)));
					itemScene.setSceneTitle(curso.getString(1));
					itemScene.setSceneIcon(curso.getString(2));
					itemScene.setSceneBackground(Integer.parseInt(curso
							.getString(3)));

					itemScene.setSceneId(Integer.parseInt(curso.getString(4)));

					itemScene.setScenePlace(curso.getString(5));
					listScene.add(itemScene);

				} while (curso.moveToNext());
			}
			return listScene;
		} finally {
			if (curso != null) {
				curso.close();
			}
			if (db != null) {
				db.close();
			}
		}
	}

	/*
	 * Get list Scene from Place
	 */

	/*
	 * Check exist scene name
	 */
	public boolean checkExistScene(SceneItem itemScene) {
		String selectQuery = "SELECT " + KEY_SCENE_ID + " FROM " + TABLE_SCENE
				+ " WHERE " + KEY_SCENE_NAME + "='" + itemScene.getSceneTitle()
				+ "'";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursorSqliteAsusHelper = db.rawQuery(selectQuery, null);
		if (cursorSqliteAsusHelper.getCount() > 0) {
			return true;
		}
		return false;
	}

	/*
	 * Add new Scene
	 */
	public int addScene(SceneItem itemScene) {
		int id = -1;
		SQLiteDatabase db = this.getReadableDatabase();
		if (db != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_SCENE_NAME, itemScene.getSceneTitle());
			values.put(KEY_SCENE_ICON, itemScene.getSceneIcon());
			values.put(KEY_SCENE_COLOR, itemScene.getSceneBackground());
			values.put(KEY_SCENE_PLACE_ID, itemScene.getPlaceId());
			values.put(KEY_SCENE_IS_DELETED, 0);
			try {
				id = (int) db.insert(TABLE_SCENE, null, values);
			} catch (Exception e) {
				id = -1;
			}
			db.close();
		}
		return id;
	}

	/*
	 * public int addScene(SceneItem itemScene) { SQLiteDatabase db =
	 * this.getReadableDatabase(); ContentValues values = new ContentValues();
	 * values.put(KEY_SCENE_NAME, itemScene.getSceneTitle());
	 * values.put(KEY_SCENE_ICON, itemScene.getSceneIcon());
	 * values.put(KEY_SCENE_COLOR, itemScene.getSceneBackground());
	 * values.put(KEY_SCENE_PLACE_ID, itemScene.getPlaceId());
	 * values.put(KEY_SCENE_IS_DELETED, 0); int id = (int)
	 * db.insert(TABLE_SCENE, null, values); if (db != null) { db.close(); }
	 * return id; }
	 */

	/*
	 * Update scene
	 */
	public void updateScene(SceneItem itemScene) {

		ContentValues values = new ContentValues();
		values.put(KEY_SCENE_NAME, itemScene.getSceneTitle());
		values.put(KEY_SCENE_ICON, itemScene.getSceneIcon());
		values.put(KEY_SCENE_COLOR, itemScene.getSceneBackground());
		values.put(KEY_SCENE_PLACE_ID, itemScene.getScenePlace());
		getWritableDatabase().update(TABLE_SCENE, values,
				KEY_SCENE_ID + " = ? ",
				new String[] { String.valueOf(itemScene.getSceneId()) });

	}

	/*
	 * Delete scene
	 */

	public void deleteScene(SceneItem itemScene) {

		getWritableDatabase().delete(TABLE_SCENE, KEY_SCENE_ID + " = ?",
				new String[] { String.valueOf(itemScene.getSceneId()) });
	}

	public void deleteDeviceItem(DeviceItem deviceItem) {
		getWritableDatabase().delete(TABLE_DEVICE, KEY_DEVICE_ID + " = ?",
				new String[] { String.valueOf(deviceItem.getDeviceId()) });
	}

	/*
	 * GET LIST ALL PLACE
	 */
	public ArrayList<SceneItem> getListScene() {
		ArrayList<SceneItem> listScene = new ArrayList<SceneItem>();
		String selectQuery = " SELECT " + KEY_SCENE_ID + ", " + KEY_SCENE_NAME
				+ ", " + KEY_SCENE_ICON + ", " + KEY_SCENE_COLOR + ", "
				+ KEY_SCENE_PLACE_ID + ", " + KEY_SCENE_IS_DELETED + " FROM "
				+ TABLE_SCENE;
		selectQuery += " ORDER BY " + KEY_SCENE_ID + " DESC";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursorSqliteAsusHelper = db.rawQuery(selectQuery, null);

		try {
			if (cursorSqliteAsusHelper.moveToFirst()) {
				do {
					SceneItem itemScene = new SceneItem();
					itemScene.setSceneId(Integer
							.parseInt(cursorSqliteAsusHelper.getString(0)));
					itemScene
							.setSceneTitle(cursorSqliteAsusHelper.getString(1));
					itemScene.setSceneIcon(cursorSqliteAsusHelper.getString(2));
					itemScene.setSceneBackground(Integer
							.parseInt(cursorSqliteAsusHelper.getString(3)));
					itemScene
							.setScenePlace(cursorSqliteAsusHelper.getString(4));
					listScene.add(itemScene);
				} while (cursorSqliteAsusHelper.moveToNext());
			}
			return listScene;
		} finally {
			if (cursorSqliteAsusHelper != null) {
				cursorSqliteAsusHelper.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	/*
	 * public ArrayList<SceneItem> getListSceneByPlaceId() {
	 * ArrayList<SceneItem> lstSceneByPlaceId =new ArrayList<SceneItem>();
	 * String selectQuery ="SELECT " + KEY_SCENE_ID + ", " + KEY_SCENE_NAME +
	 * KEY_SCENE_ICON + ", " + KEY_SCENE_COLOR + ", " + ", " +
	 * KEY_SCENE_PLACE_ID + ", " + KEY_SCENE_IS_DELETED + " FROM " + TABLE_SCENE
	 * + " LEFT OUTER JOIN " + TABLE_PLACE + " ON " + TABLE_PLACE + "." +
	 * KEY_PLACE_ID + " = " + TABLE_SCENE + "." + KEY_SCENE_PLACE_ID;
	 * selectQuery += " WHERE " + TABLE_SCENE + "." + KEY_PLACE_IS_DELETED +
	 * " = 0 "; selectQuery += " ORDER BY " + KEY_SCENE_ID + " DESC";
	 * SQLiteDatabase db = this.getReadableDatabase(); Cursor
	 * cursorSqliteAsusHelper = db.rawQuery(selectQuery, null); try { if
	 * (cursorSqliteAsusHelper.moveToFirst()) { do { SceneItem itemScene = new
	 * SceneItem(); itemScene.setSceneId(Integer
	 * .parseInt(cursorSqliteAsusHelper.getString(0))); itemScene
	 * .setSceneTitle(cursorSqliteAsusHelper.getString(1));
	 * itemScene.setSceneIcon(cursorSqliteAsusHelper.getString(2));
	 * itemScene.setSceneBackground(Integer
	 * .parseInt(cursorSqliteAsusHelper.getString(3))); itemScene
	 * .setScenePlace(cursorSqliteAsusHelper.getString(4));
	 * lstSceneByPlaceId.add(itemScene); } while
	 * (cursorSqliteAsusHelper.moveToNext()); } return lstSceneByPlaceId; }
	 * finally { if (cursorSqliteAsusHelper != null) {
	 * cursorSqliteAsusHelper.close(); } if (db != null) { db.close(); } } }
	 */
	/*
	 * Add new Wifi
	 */
	public void addWifi(WifiItem item) {
		SQLiteDatabase db = this.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put(KEY_WIFI_NAME, item.getWifiName());
		values.put(KEY_WIFI_PASSWORD, item.getWifiPassword());
		values.put(KEY_WIFI_CODE, item.getWifiCode());
		values.put(KEY_WIFI_IS_DELETED, 0);

		db.insert(TABLE_WIFI, null, values);
		if (db != null) {
			db.close();
		}
	}

	/*
	 * get list wifi
	 */
	public ArrayList<WifiItem> getListWifi() {
		ArrayList<WifiItem> list = new ArrayList<WifiItem>();
		String selectQuery = "SELECT " + TABLE_WIFI + "." + KEY_WIFI_ID + ", "
				+ TABLE_WIFI + "." + KEY_WIFI_NAME + ", " + TABLE_WIFI + "."
				+ KEY_WIFI_PASSWORD + ", " + TABLE_WIFI + "." + KEY_WIFI_CODE
				+ " FROM " + TABLE_WIFI;

		selectQuery += " WHERE " + TABLE_WIFI + "." + KEY_WIFI_IS_DELETED
				+ " = 0 ";

		selectQuery += " ORDER BY " + TABLE_WIFI + "." + KEY_WIFI_ID + " DESC";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursorSqliteAsusHelper = db.rawQuery(selectQuery, null);

		try {
			if (cursorSqliteAsusHelper.moveToFirst()) {
				do {
					WifiItem item = new WifiItem();
					item.setWifiId(Integer.parseInt(cursorSqliteAsusHelper
							.getString(0)));
					item.setWifiName(cursorSqliteAsusHelper.getString(1));
					item.setWifiPassword(cursorSqliteAsusHelper.getString(2));
					item.setWifiCode(cursorSqliteAsusHelper.getString(3));
					list.add(item);
				} while (cursorSqliteAsusHelper.moveToNext());
			}
			return list;
		} finally {
			if (cursorSqliteAsusHelper != null) {
				cursorSqliteAsusHelper.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	/*
	 * Add new box
	 */
	public void addBox(BoxItem item) {
		SQLiteDatabase db = this.getReadableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_BOX_NAME, item.getBoxName());
		values.put(KEY_BOX_PASSWORD, item.getBoxPassword());
		values.put(KEY_BOX_CODE, item.getBoxCode());
		values.put(KEY_BOX_WIFI_ID, item.getWifiId());
		values.put(KEY_BOX_IS_DELETED, 0);

		try {
			db.insert(TABLE_BOX, null, values);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		if (db != null) {
			db.close();
		}
	}

	public void editNamebox(BoxItem boxItem) {

		ContentValues values = new ContentValues();
		values.put(KEY_BOX_NAME, boxItem.getBoxName());

		// values.put(KEY_BOX_ID, boxItem.getBoxId());
		getWritableDatabase().update(TABLE_BOX, values,
				KEY_BOX_ID + " = " + String.valueOf(boxItem.getBoxId()), null);

	}

	/*
	 * get list box
	 */
	public ArrayList<BoxItem> getListBox(int wifiId) {
		ArrayList<BoxItem> list = new ArrayList<BoxItem>();
		String selectQuery = "SELECT " + TABLE_BOX + "." + KEY_BOX_ID + ", "
				+ TABLE_BOX + "." + KEY_BOX_NAME + ", " + TABLE_BOX + "."
				+ KEY_BOX_PASSWORD + ", " + TABLE_BOX + "." + KEY_BOX_CODE
				+ "," + TABLE_BOX + "." + KEY_BOX_WIFI_ID + ", " + TABLE_WIFI
				+ "." + KEY_WIFI_NAME + ", " + " FROM " + TABLE_BOX

				+ " LEFT OUTER JOIN " + TABLE_WIFI + " ON " + TABLE_WIFI + "."
				+ KEY_WIFI_ID + " = " + TABLE_BOX + "." + KEY_BOX_WIFI_ID;

		selectQuery += " WHERE " + TABLE_BOX + "." + KEY_BOX_IS_DELETED
				+ " = 0 ";
		selectQuery += " AND " + TABLE_BOX + "." + KEY_BOX_WIFI_ID + " = "
				+ wifiId;

		selectQuery += " ORDER BY " + TABLE_BOX + "." + KEY_BOX_ID + " DESC";

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursorSqliteAsusHelper = db.rawQuery(selectQuery, null);

		try {
			if (cursorSqliteAsusHelper.moveToFirst()) {
				do {
					BoxItem item = new BoxItem();
					item.setBoxId(Integer.parseInt(cursorSqliteAsusHelper
							.getString(0)));
					item.setBoxName(cursorSqliteAsusHelper.getString(1));
					item.setBoxPassword(cursorSqliteAsusHelper.getString(2));
					item.setBoxCode(cursorSqliteAsusHelper.getString(3));
					item.setWifiId(Integer.parseInt(cursorSqliteAsusHelper
							.getString(4)));
					item.setWifiName(cursorSqliteAsusHelper.getString(5));
					list.add(item);
				} while (cursorSqliteAsusHelper.moveToNext());
			}
			return list;
		} finally {
			if (cursorSqliteAsusHelper != null) {
				cursorSqliteAsusHelper.close();
			}
			if (db != null) {
				db.close();
			}
		}

	}

	/*
	 * update place_id for Device with device_id
	 */
	public void updatePlaceId(int deviceId, int placeId) {
		String sql = "UPDATE " + TABLE_DEVICE + " SET " + KEY_DEVICE_PLACE_ID
				+ " = " + placeId + " WHERE " + KEY_DEVICE_ID + " = "
				+ deviceId;
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sql);

	}

	/*
	 * Update placeId for list Device
	 */
	public void updatePlaceIdForListDevice(ArrayList<DeviceItem> listDevice,
			int placeId) {

		// Log.e("TEST_TEST_itemPlace", "TEST_TEST_itemPlace: " +
		// itemPlace.getPlaceId());

		if (listDevice.size() > 0) {
			SQLiteDatabase db = this.getReadableDatabase();
			if (db != null) {
				String whereClause = "";

				for (DeviceItem deviceItem : listDevice) {

					whereClause += "'" + deviceItem.getDeviceId() + " ' ,";
				}
				whereClause = whereClause
						.substring(0, whereClause.length() - 1);
				whereClause = KEY_DEVICE_ID + " IN  (" + whereClause + ") ";

				ContentValues values = new ContentValues();

				values.put(KEY_DEVICE_PLACE_ID, placeId);

				try {
					db.update(TABLE_DEVICE, values, whereClause, null);
				} catch (Exception e) {
					Log.e("TEST_TEST_UPDATE_SUCCESS",
							"TEST_TEST_UPDATE_SUCCESS:" + e.toString());
				}

				db.close();
			}

		}
	}

	/*
	 * update place_id for device with old place_id
	 */
	public void updatePlaceIdNew(int oldplaceId, int newplaceId) {
		String sql = "UPDATE " + TABLE_DEVICE + " SET " + KEY_DEVICE_PLACE_ID
				+ " = " + newplaceId + " WHERE " + KEY_DEVICE_ID + " = "
				+ oldplaceId;
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sql);

	}

	/*
	 * Delete place card
	 */
	public void deletePlace(int placeId) {
		String sqlCommand = "DELETE FROM " + TABLE_PLACE + " WHERE "
				+ KEY_PLACE_ID + "=" + placeId;
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sqlCommand);
	}

	/*
	 * public int addUser(UserItem userItem) { int id = -1; SQLiteDatabase db =
	 * this.getReadableDatabase(); if (db != null) { ContentValues values = new
	 * ContentValues(); values.put(KEY_USER_NAME, userItem.getUserName());
	 * values.put(KEY_USER_EMAIL, userItem.getUserEmail());
	 * values.put(KEY_USER_ICON, userItem.getUserIcon());
	 * values.put(KEY_USER_STATUS, userItem.getUserStatus());
	 * values.put(KEY_USER_IS_DELETED, 0); try { id = (int)
	 * db.insert(TABLE_USER, null, values); } catch (Exception e) { id = -1; }
	 * db.close(); } return id; }
	 */

	public void updateDeviceStatus(int oldStatus, int newStatus) {
		String sqlDeviceStatus = "UPDATE " + TABLE_DEVICE + " SET "
				+ KEY_DEVICE_STATUS + " = " + newStatus + " WHERE "
				+ KEY_DEVICE_STATUS + " = " + oldStatus;
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sqlDeviceStatus);
	}

	/*
	 * update place_id for device with old place_id
	 */

	public void updatePlaceToUncategory(int oldplaceId, int checkIsDelete) {
		String sql = "UPDATE " + TABLE_PLACE + " SET " + KEY_PLACE_IS_DELETED
				+ " = " + checkIsDelete + " WHERE " + KEY_PLACE_ID + " = "
				+ oldplaceId;
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sql);

	}

}
